﻿#include "StdAfx.h"
#include "ControlFactory.h"

namespace DuiLib {

    CControlFactory *CControlFactory::m_pThis = NULL;

    CControlFactory::CControlFactory() {
        INNER_REGISTER_DUICONTROL(CControlUI);
        INNER_REGISTER_DUICONTROL(CContainerUI);
        INNER_REGISTER_DUICONTROL(CButtonUI);
        INNER_REGISTER_DUICONTROL(CComboBoxUI);
        INNER_REGISTER_DUICONTROL(CComboBoxUI);
        INNER_REGISTER_DUICONTROL(CDateTimeUI);
        INNER_REGISTER_DUICONTROL(CEditUI);
        INNER_REGISTER_DUICONTROL(CActiveXUI);
        INNER_REGISTER_DUICONTROL(CFlashUI);
        INNER_REGISTER_DUICONTROL(CGifAnimUI);
        INNER_REGISTER_DUICONTROL(CGroupBoxUI);
        INNER_REGISTER_DUICONTROL(CIPAddressUI);
        INNER_REGISTER_DUICONTROL(CLabelUI);
        INNER_REGISTER_DUICONTROL(CListUI);
        INNER_REGISTER_DUICONTROL(CListHeaderUI);
        INNER_REGISTER_DUICONTROL(CListHeaderItemUI);
        INNER_REGISTER_DUICONTROL(CListLabelElementUI);
        INNER_REGISTER_DUICONTROL(CListTextElementUI);
        INNER_REGISTER_DUICONTROL(CListContainerElementUI);
        INNER_REGISTER_DUICONTROL(CMenuUI);
        INNER_REGISTER_DUICONTROL(CMenuElementUI);
        INNER_REGISTER_DUICONTROL(COptionUI);
        INNER_REGISTER_DUICONTROL(CCheckBoxUI);
        INNER_REGISTER_DUICONTROL(CProgressUI);
        INNER_REGISTER_DUICONTROL(CRichEditUI);
        INNER_REGISTER_DUICONTROL(CScrollBarUI);
        INNER_REGISTER_DUICONTROL(CSliderUI);
        INNER_REGISTER_DUICONTROL(CTextUI);
        INNER_REGISTER_DUICONTROL(CTreeNodeUI);
        INNER_REGISTER_DUICONTROL(CTreeViewUI);
        INNER_REGISTER_DUICONTROL(CWebBrowserUI);
        INNER_REGISTER_DUICONTROL(CAnimationTabLayoutUI);
        INNER_REGISTER_DUICONTROL(CChildLayoutUI);
        INNER_REGISTER_DUICONTROL(CHorizontalLayoutUI);
        INNER_REGISTER_DUICONTROL(CTabLayoutUI);
        INNER_REGISTER_DUICONTROL(CTileLayoutUI);
        INNER_REGISTER_DUICONTROL(CVerticalLayoutUI);
        INNER_REGISTER_DUICONTROL(CRollTextUI);
        INNER_REGISTER_DUICONTROL(CColorPaletteUI);
        INNER_REGISTER_DUICONTROL(CHotKeyUI);
        INNER_REGISTER_DUICONTROL(CFadeButtonUI);
        INNER_REGISTER_DUICONTROL(CRingUI);
        INNER_REGISTER_DUICONTROL(CPlaceHolderUI);
#ifdef UILIB_WITH_CEF
        INNER_REGISTER_DUICONTROL(CCefUI);
#endif
    }

    CControlFactory::~CControlFactory() {
    }

    CControlUI *CControlFactory::CreateControl(CDuiString strClassName) {
        strClassName.MakeLower();
        MAP_DUI_CTRATECLASS::iterator iter = m_mapControl.find(strClassName);

        if ( iter == m_mapControl.end()) {
            return NULL;
        } else {
            return (CControlUI *) (iter->second());
        }
    }

    void CControlFactory::RegistControl(CDuiString strClassName, CreateClass pFunc) {
        strClassName.MakeLower();
        m_mapControl.insert(MAP_DUI_CTRATECLASS::value_type(strClassName, pFunc));
    }

    CControlFactory *CControlFactory::GetInstance() {
        if (!m_pThis) {
            if (!m_pThis) {
                m_pThis = new CControlFactory;
            }
        }
        return m_pThis;
    }

    void CControlFactory::Release() {
        if (m_pThis) {
            delete m_pThis;
            m_pThis = NULL;
        }
    }
}